//
//  HNSH_AddressCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/26.
//

#import "HNSH_AddressCell.h"

@interface HNSH_AddressCell ()
/**  */
@property (nonatomic, strong)UIView * bgView;
/**  */
@property (nonatomic, strong)UILabel * nameLabel;
/**  */
@property (nonatomic, strong)UILabel * phoneLabel;
/**  */
@property (nonatomic, strong)UILabel * addressLabel;
/**  */
@property (nonatomic, strong)UIButton * selBtn;
/**  */
@property (nonatomic, strong)UILabel * editLabel;
/**  */
@property (nonatomic, strong)UIView * segView;
/**  */
@property (nonatomic, strong)UILabel * delLabel;
@end

@implementation HNSH_AddressCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor appBGColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.nameLabel];
    [self.bgView addSubview:self.phoneLabel];
    [self.bgView addSubview:self.addressLabel];
    [self.bgView addSubview:self.selBtn];
    [self.bgView addSubview:self.editLabel];
    [self.bgView addSubview:self.segView];
    [self.bgView addSubview:self.delLabel];
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(12);
        make.height.mas_equalTo(22);
    }];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(18);
        make.top.height.mas_equalTo(self.nameLabel);
    }];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(17);
        make.bottom.mas_equalTo(-63);
    }];
    [self.selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
    }];
    [self.delLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(40);
        make.top.height.mas_equalTo(self.selBtn);
    }];
    [self.segView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.delLabel.mas_left).offset(0);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(15);
        make.centerY.mas_equalTo(self.delLabel);
    }];
    [self.editLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.segView.mas_left).offset(0);
        make.top.height.mas_equalTo(self.delLabel);
        make.width.mas_equalTo(40);
    }];
}

- (void)selBtnAction{
    if (self.editAddressBlock) {
        self.editAddressBlock(0, self);
    }
}

- (void)gesAction:(UITapGestureRecognizer *)sender{
    NSInteger tag = sender.view.tag - 1000;
    if (self.editAddressBlock) {
        self.editAddressBlock(tag, self);
    }
}

//MARK: Setter Methods
- (void)setModel:(HNSH_AddressModel *)model{
    _model = model;
    
    self.nameLabel.text = model.name;
    self.phoneLabel.text = model.phone;
    self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@", model.province_name, model.city_name, model.county_name, model.details];
    self.selBtn.selected = model.default_address;
}

//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        _bgView.layer.cornerRadius = 8;
        _bgView.layer.shadowOffset = CGSizeMake(0, 3);
        _bgView.layer.shadowOpacity = 1;
        _bgView.layer.shadowRadius = 6;
        _bgView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1600].CGColor;
    }
    return _bgView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont boldSystemFontOfSize:16];
        _nameLabel.textColor = [UIColor appBlack333LabelColor];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel{
    if (!_phoneLabel) {
        _phoneLabel = [UILabel new];
        _phoneLabel.font = [UIFont systemFontOfSize:14];
        _phoneLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _phoneLabel;
}

- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [UILabel new];
        _addressLabel.font = [UIFont systemFontOfSize:14];
        _addressLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        _addressLabel.numberOfLines = 0;
    }
    return _addressLabel;
}

- (UIButton *)selBtn{
    if (!_selBtn) {
        _selBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selBtn setTitle:@" 默认地址" forState:UIControlStateNormal];
        [_selBtn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [_selBtn setImage:[UIImage imageNamed:@"base_select_off"] forState:UIControlStateNormal];
        [_selBtn setImage:[UIImage imageNamed:@"base_select_on"] forState:UIControlStateSelected];
        _selBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _selBtn.userInteractionEnabled = NO;
    }
    return _selBtn;
}

- (UILabel *)delLabel{
    if (!_delLabel) {
        _delLabel = [UILabel new];
        _delLabel.font = [UIFont systemFontOfSize:14];
        _delLabel.textColor = [UIColor appGrayLabelColor];
        _delLabel.text = @"删除";
        _delLabel.userInteractionEnabled = YES;
        _delLabel.textAlignment = NSTextAlignmentCenter;
        _delLabel.tag = 1001;
        
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction:)];
        [_delLabel addGestureRecognizer:ges];
    }
    return _delLabel;
}

- (UIView *)segView{
    if (!_segView) {
        _segView = [UIView new];
        _segView.backgroundColor = [UIColor appGrayLabelColor];
    }
    return _segView;
}

- (UILabel *)editLabel{
    if (!_editLabel) {
        _editLabel = [UILabel new];
        _editLabel.font = [UIFont systemFontOfSize:14];
        _editLabel.textColor = [UIColor appGreenColor];
        _editLabel.text = @"编辑";
        _editLabel.userInteractionEnabled = YES;
        _editLabel.textAlignment = NSTextAlignmentCenter;
        _editLabel.tag = 1002;
        
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction:)];
        [_editLabel addGestureRecognizer:ges];
    }
    return _editLabel;
}

@end
